<template>
  <a-card :bordered="false">
    <div class="card-title">{{ $route.meta.title }}</div>
    <div class="table-operator">
      <!-- 操作板块 -->
      <div class="row-item-tab clearfix">
        <a-button v-action:add type="primary" icon="plus" @click="handleAdd">新增</a-button>
        <a-button v-action:export type="primary" icon="download" :loading="isExporting" @click="handleExport"
          >导出</a-button
        >
      </div>
    </div>
    <s-table ref="table" rowKey="user_id" :loading="isLoading" :columns="columns" :data="loadData" :pageSize="15">
      <!-- 用户来源 -->
      <span slot="source" slot-scope="text">
        <a-tag :color="text === 10 ? 'green' : ''">{{ getUserSource(text) }}</a-tag>
      </span>
      <!-- 操作项 -->
      <span slot="action" slot-scope="text, item">
        <a v-action:edit style="margin-right: 8px" @click="handleEdit(item)">编辑</a>
        <a v-action:delete @click="handleDelete(item)">删除</a>
      </span>
    </s-table>
    <AddForm ref="AddForm" :categoryList="categoryList" @handleSubmit="handleRefresh" />
    <EditForm ref="EditForm" :categoryList="categoryList" @handleSubmit="handleRefresh" />
  </a-card>
</template>

<script>
import * as Api from '@/api/appointment/user'
import UserSourceEnum from '@/common/enum/appointment/UserSource'
import { ContentHeader, STable } from '@/components'
import EditForm from './modules/EditForm'
import AddForm from './modules/AddForm'

// 表格表头
const columns = [
  {
    title: 'ID',
    dataIndex: 'user_id',
    align: 'center',
  },
  {
    title: '用户姓名',
    dataIndex: 'nick_name',
    scopedSlots: { customRender: 'nick_name' },
    align: 'center',
  },
  {
    title: '手机号',
    dataIndex: 'mobile',
    scopedSlots: { customRender: 'mobile' },
    align: 'center',
  },
  {
    title: '用户来源',
    dataIndex: 'source',
    scopedSlots: { customRender: 'source' },
    align: 'center',
  },
  {
    title: '操作',
    dataIndex: 'action',
    width: '180px',
    scopedSlots: { customRender: 'action' },
    align: 'center',
  },
]

export default {
  name: 'Index',
  components: {
    ContentHeader,
    STable,
    AddForm,
    EditForm,
  },
  data() {
    return {
      expand: false,
      // 当前表单元素
      searchForm: this.$form.createForm(this),
      // 分类列表
      categoryList: [],
      // 查询参数
      queryParam: {},
      // 正在加载
      isLoading: false,
      // 正在导出
      isExporting: false,
      // 表头
      columns,
      // 加载数据方法 必须为 Promise 对象
      loadData: (param) => {
        return Api.list({ ...param, ...this.queryParam }).then((response) => {
          return response.data.list
        })
      },
    }
  },
  methods: {
    getUserSource(value) {
      return UserSourceEnum.getNameByValue(value)
    },
    /**
     * 删除记录
     */
    handleDelete(item) {
      const app = this
      const modal = this.$confirm({
        title: '您确定要删除该记录吗?',
        content: '删除后不可恢复',
        onOk() {
          return Api.deleted({ userId: item.user_id })
            .then((result) => {
              app.$message.success(result.message, 1.5)
              app.handleRefresh()
            })
            .finally((result) => {
              modal.destroy()
            })
        },
      })
    },

    /**
     * 新增记录
     */
    handleAdd() {
      this.$refs.AddForm.add()
    },

    /**
     * 编辑记录
     */
    handleEdit(item) {
      this.$refs.EditForm.edit(item.user_id)
    },

    /**
     * 刷新列表
     * @param Boolean bool 强制刷新到第一页
     */
    handleRefresh(bool = false) {
      this.$refs.table.refresh(bool)
    },

    /**
     * 导出数据
     */
    handleExport() {
      Api.exportData({
        columns: ['user_id', 'nick_name', 'mobile', 'source'],
      }).then((res) => {
        window.open(res.data.downloadPath)
      })
    },
  },
}
</script>
<style lang="less" scoped>
.ant-card-body {
  padding: 22px 29px 25px;
}
// 筛选tab
.tab-list {
  margin-right: 20px;
}

.ant-tag {
  margin-right: 0 !important;
}
</style>
